home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / BPAS9.ARJ / 3_2.PAS < prev    next >
Pascal/Delphi Source File  |  1991-09-07  |  10KB  |  265 lines

  1. {---------------------------------------------------------------------
  2.  PROGRAM: 3_2
  3.  
  4.  This program uses the template to build a few procedures quickly,
  5.  the programmer is able to copy and fill in the NULL.PROC procedure
  6.  template with a procedure or function and rename it as needed, moving the
  7.  new procedure to its alphabetical place in the procedures, and placing
  8.  the procedure name in the MAIN PROGGRAM so that it is called in its
  9.  turn.
  10.  
  11.  However, what if the programmer wants to call a procedure or function
  12.  when he wants to?  What if he does not want the whole program to run
  13.  all the time, but wants to give the user options of which part of the
  14.  program to run?
  15.  
  16.  What is new in this template is the placing of a menu in the main
  17.  program so that the user can call the procedure/function as needed and
  18.  as desired.
  19.  
  20.  The program still uses INIT, and USES the CRT Unit to ClrScr
  21.  at initiation.  It also adds READLN to the end of the program
  22.  so that, when it is run, the output is sent to the screen and stops so
  23.  that the programmer is allowed to see the output without pressing ALT F5.
  24.  
  25.  Author:                    Mike Benedict
  26.  Date Started:              9/01/91
  27.  Latest Revision:           9/01/91
  28.  Version:                   Turbo Pascal 6.0
  29.  
  30.  -------------------------------------------------------------------}
  31.  
  32.  PROGRAM Lesson3_2;
  33.  
  34.  USES                                      { USES is a reserved word that  }
  35.    Crt;                                    { tells the compiler to use one }
  36.                                            { of the standard libraries or  }
  37.                                            { units listed in the Pascal    }
  38.                                            { Programmer's Guide.           }
  39.  
  40.  
  41.  VAR
  42.    Choice     :  Char;                     { Used in CASE Statements     }
  43.    UserQuits  :  Boolean;                  { Used to Quit Menu & Program }
  44.  
  45.  
  46.  
  47.  {------------------------------------}    { Anything between brackets  }
  48.  {            PROCEDURES              }    { is ignored at compile time }
  49.  {------------------------------------}    { and allows clear comments  }
  50.                                            { within source code.        }
  51.  
  52.  {-----------------------------}
  53.  {         ASCII.PROC          }
  54.  {-----------------------------}
  55.  
  56.  PROCEDURE ASCII;
  57.                                            { See Tom Swan p. 39 ff }
  58.  
  59.  VAR
  60.    Ch  :  Char;
  61.    Num :  Integer;
  62.  
  63.  BEGIN
  64.    ClrScr;
  65.    WriteLn;
  66.    WriteLn;
  67.    Write    ( 'ASCII Character Set  ' );
  68.    WriteLn;
  69.    WriteLn;
  70.    FOR Num := 0 TO 255 DO
  71.      WriteLn ( 'ASCII Number = ',Num, ' Character = ',Char(Num) );
  72.    ReadLn;
  73.  END;
  74.                                              { As an exercise, convert this procedure to
  75.                                                one that prints out the ASCII code.  See
  76.                                                Tom Swan pp. 587-588 to declare a file,
  77.                                                then make the file LPT1. )
  78.  {-----------------------------}
  79.  {          INIT.PROC          }
  80.  {-----------------------------}
  81.  
  82.  PROCEDURE Init;
  83.  
  84.  BEGIN
  85.    TextBackground(Blue);                   { Procedures and functions that   }
  86.    TextColor(White);                       { are pre-defined by Borland's    }
  87.    ClrScr;                                 { Reference manual.  They are     }
  88.  END;                                      { called the Run-Time Library.    }
  89.  
  90.  
  91.  {-----------------------------}
  92.  {       RANGECHK.PROC         }
  93.  {-----------------------------}
  94.  
  95.  PROCEDURE RangeChk;
  96.                                            { See Tom Swan p. 25 ff }
  97.  
  98.  VAR
  99.    Age  :  0..100;
  100.  
  101.  BEGIN
  102.    ClrScr;
  103.    WriteLn;
  104.    WriteLn;
  105.    Write    ( 'How old are you?  ' );
  106.    {$R+}                                      { Compiler Directive: Turn Range Checking On }
  107.    ReadLn   ( Age );
  108.    {$R-}                                      { Compiler Directive: Turn Range Checking Off }
  109.    WriteLn;
  110.    WriteLn;
  111.    WriteLn  ( 'You are ', Age, ' years old.' );
  112.    ReadLn;
  113.  
  114.  END;
  115.  
  116.  
  117.  {-----------------------------}
  118.  {       RANGECHK2.PROC        }
  119.  {-----------------------------}
  120.  
  121.  PROCEDURE RangeChk2;
  122.  
  123.  VAR
  124.    Age  :  0..100;
  125.  
  126.  BEGIN
  127.    ClrScr;
  128.    WriteLn;
  129.    WriteLn;
  130.    Write    ( 'How old are you?  ' );
  131. {$R-}                                      { Turn Range Checking Off }
  132.    ReadLn   ( Age );
  133.      IF (Age) > 100 THEN
  134.        WriteLn ('Age is too high.  See value below and then re-enter. ')
  135.      ELSE
  136.      IF (Age) < 0 THEN
  137.        WriteLn ('Age is too low.  See value below and then re-enter.  ')
  138.      ELSE
  139. {$R+}                                      { Turn Range Checking On  }
  140.        WriteLn;
  141.        WriteLn;
  142.        WriteLn  ( 'You are ', Age, ' years old.' );
  143.    ReadLn;
  144.  
  145.  END;
  146.  
  147.  
  148.  {-----------------------------}
  149.  {       VARCONST.PROC         }
  150.  {-----------------------------}
  151.  
  152.  PROCEDURE VarConst;
  153.  
  154.                                            { This repeats the lesson given by
  155.                                              Noe Lopez regarding Variable
  156.                                              Constants.  See Tom Swan pp. 31-32
  157.                                              for the same thing.
  158.  
  159.                                              A variable constant is a static
  160.                                              variable, or typed constant.
  161.  
  162.                                              It allows the programmer to combine
  163.                                              constants and variables and save
  164.                                              memory.  The programmer pre-initializes
  165.                                              a variable as a constant and then
  166.                                              change it.}
  167.  
  168.  
  169.  
  170.  CONST
  171.    MiscString  :  String[10] = 'Lesson 2';
  172.  
  173.  
  174.  BEGIN
  175.    ClrScr;                                { This ClrScr clears the menu off the screen. }
  176.    WriteLn ( 'This is ', (MiscString) );
  177.    WriteLn;
  178.    WriteLn ( 'If not, please enter the correct Lesson.');
  179.    ReadLn  ( MiscString );
  180.    WriteLn;
  181.    WriteLn ( 'The correct lesson is ',(MiscString),'.');
  182.    ReadLn;                                { This ReadLn keeps the program from
  183.                                             returning to the menu immediately.
  184.                                             Try modifying it by commenting out
  185.                                             the ReadLn, or deleting it. }
  186.  END;
  187.  
  188.                                           { MiscString is a constant that is
  189.                                             assigned a variable value.  In the
  190.                                             course of the procedure, the user
  191.                                             interacts and the program reads in
  192.                                             the new value. }
  193.  
  194.  
  195.  {-----------------------------}
  196.  {         .PROC          }
  197.  {-----------------------------}
  198.  
  199.  PROCEDURE Null;
  200.  
  201.  BEGIN
  202.  END;
  203.  
  204.  {------------------------------------}
  205.  {           MAIN PROGRAM             }
  206.  {------------------------------------}
  207.  
  208.   BEGIN
  209.     Init;                                   { Procedure Call }
  210.  
  211.     UserQuits := False;                     { Boolean Variable set to false }
  212.  
  213.     REPEAT                                  { Note that REPEAT UNTIL contains all the
  214.                                               WriteLn statements of the Menu, then the
  215.                                               ReadLn statement to capture the input,
  216.                                               before the CASE END block. }
  217.       ClrScr;
  218.       WriteLn;
  219.       WriteLn  ( '                    Welcome to Beginners Pascal');
  220.       WriteLn;
  221.       WriteLn  ( '                               MENU');
  222.       WriteLn;
  223.       WriteLn  ( '                       A.  Variable Constant ' );
  224.       WriteLn  ( '                       B.  Range Check with Error Code  ' );
  225.       WriteLn  ( '                       C.  Range Check with Program Error Message ' );
  226.       WriteLn  ( '                       D.  ASCII Code ' );
  227.       WriteLn  ( '                       Q.  User Quits Menu'  );
  228.       WriteLn;
  229.       WriteLn;
  230.  
  231.       Write    ( 'Select a menu choice:  ' );
  232.       ReadLn   ( Choice );
  233.                                            { The CASE ___ OF statement uses the variable,
  234.                                              in this case "Choice" to capture the input,
  235.                                              then executes that procedure/function. }
  236.  
  237.                                            { Notice that after a choice, the loop returns
  238.                                              back to the Menu. }
  239.       CASE Choice OF
  240.         'a', 'A'    :  VarConst;           { Choice A calls Procedure VarConst }
  241.         'b', 'B'    :  RangeChk;           { Choice B calls Procedure RangeChk }
  242.         'c', 'C'    :  RangeChk2;          { Etc. }
  243.         'd', 'D'    :  ASCII;
  244.         'e', 'E'    :  Null;               { Choice calls Null.Proc until assigned }
  245.  
  246.  
  247.  
  248.         'q', 'Q'    :  UserQuits := True;  { Choice Q changes boolean value to False
  249.                                              and exits Repeat Until Loop and goes to
  250.                                              next line of Main Program code.  That is,
  251.                                              in this program it stops repeating the
  252.                                              menu and drops the user to a cursor.  When
  253.                                              you press <ENTER>, it exits the program.
  254.                                              Change the program to exit immediately.}
  255.  
  256.       END
  257.     UNTIL UserQuits;
  258.  
  259.     ReadLn;
  260.   END.                                     { ReadLn stops the program for user  }
  261.                                            { input.           This is a common  }
  262.                                            { way to get the program to stop and }
  263.                                            { show you the screen without pres-  }
  264.                                            { sing ALT-F5.                       }
  265.